package com.example.spring_trans.controller;

import com.example.spring_trans.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * ClassName: TransController
 * Package: IntelliJ IDEA
 * Description:
 *
 * @Author 谷京京
 * @Create 2024/3/19 22:45
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/teans")
public class TransController {
    @Autowired
    private UserService userService;

    /**
     * rollbackFor = {Exception.class, Error.class}
     * 发生异常, 事务回滚
     * @param userName
     * @param password
     * @return
     */
    @Transactional(rollbackFor = {Exception.class, Error.class}, isolation = Isolation.READ_COMMITTED)
    @RequestMapping("/registry")
    public String registry(String userName, String password) {
        Integer result = userService.insertUser(userName, password);
        log.info("用户插入成功, result:"+result);
        return "注册成功";
    }
}
